#pragma once
#include "Protocol.hpp"

class Calculator
{
public:
    Calculator(int x = 0, int y = 0, int oper = '=')
        : _x(x),
          _y(y),
          _oper(oper),
          _result(0),
          _code(0)
    {

    }

    Response Execute(Request req)
    {
        Response res_out;

        _x = req.X();
        _y = req.Y();
        _oper = req.Oper();
        switch (_oper)
        {
        case '-':
            _result = _x - _y;
            break;
        case '+':
            _result = _x + _y;
            break;
        case '*':
            _result = _x * _y;
            break;
        case '/':
            if (_y == 0)
            {
                _code = 1;
                break;
            }
            _result = _x / _y;
            break;
        case '^':
            _result = _x ^ _y;
            break;
        default:
            _code = 2;
            _result = 0;
            break;
        }

        res_out.SetCode(_code);
        res_out.SetResult(_result);
        return res_out;
    }

private:
    int _x;
    int _y;
    int _oper;

    int _result;
    int _code;
};